package com.sprucetec.live.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sprucetec.live.common.dto.PageDTO;
import com.sprucetec.live.common.dto.PagedListDTO;
import com.sprucetec.live.common.dto.business.LiveIntranetIpDto;
import com.sprucetec.live.constants.ZkCacheConstant;
import com.sprucetec.live.entity.LiveIntranetIp;
import com.sprucetec.live.enums.WatchTypeEnum;
import com.sprucetec.live.mapper.LiveIntranetIpMapper;
import com.sprucetec.live.service.LiveIntranetIpService;
import com.sprucetec.live.utils.ZkCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName LiveIntranetIpServiceImpl
 * @Description:
 * @Author Kenny
 * @Date 2020/4/26
 **/
@Service("liveIntranetIpService")
@Slf4j
public class LiveIntranetIpServiceImpl implements LiveIntranetIpService {

    @Autowired
    private LiveIntranetIpMapper liveIntranetIpMapper;

    @Autowired
    private ZkCacheUtil zkCacheUtil;


    @Override
    public void addLiveIp(LiveIntranetIp liveIntranetIp) {
        liveIntranetIpMapper.insertOrUpdateSelective(liveIntranetIp);
        notifyJvmIpCache();
    }

    @Override
    public void updateLiveIp(LiveIntranetIp liveIntranetIp) {
        liveIntranetIpMapper.insertOrUpdateSelective(liveIntranetIp);
        notifyJvmIpCache();
    }

    @Override
    public void deleteLiveIp(Long id) {
        liveIntranetIpMapper.deleteByPrimaryKey(id);
        notifyJvmIpCache();
    }

    @Override
    public List<LiveIntranetIp> getLiveList() {
        return liveIntranetIpMapper.selectByExample(new Example(LiveIntranetIp.class));
    }


    private void notifyJvmIpCache() {
        try {
            zkCacheUtil.setWatcher(ZkCacheConstant.INTRANET_IP_PREFIX, WatchTypeEnum.INTRANET_IP.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public PagedListDTO<LiveIntranetIp> getLiveIpListPage(LiveIntranetIpDto liveIntranetIpDto) {

        PagedListDTO<LiveIntranetIp> pagedListDTO = new PagedListDTO<>();
        PageHelper.startPage(liveIntranetIpDto.getPageIndex(), liveIntranetIpDto.getPageSize());

        Example example = new Example(LiveIntranetIp.class);
        if (StringUtils.isNotBlank(liveIntranetIpDto.getIp())) {
            example.createCriteria().andLike("ip", "%" + liveIntranetIpDto.getIp() + "%");
        }
        List<LiveIntranetIp> liveIntranetIps = liveIntranetIpMapper.selectByExample(example);
        PageInfo<LiveIntranetIp> pages = new PageInfo(liveIntranetIps);

        pagedListDTO.setTotal((int) pages.getTotal());
        pagedListDTO.setRecords(liveIntranetIps);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageSize(liveIntranetIpDto.getPageSize());
        pageDTO.setPageIndex(liveIntranetIpDto.getPageIndex());
        pagedListDTO.setPage(pageDTO);
        return pagedListDTO;
    }
}
